package com.hj.saapp.control.activity;

import android.app.Activity;
import android.os.Bundle;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.hj.saapp.R;
import com.hj.saapp.common.manager.AppManager;
import com.hj.saapp.control.activity.base.BaseActivity;


/**
 * author:丁永泽
 * 默认弹窗
 */
public class DefaultDialog extends Activity implements View.OnClickListener{

    private TextView titleTv;

    private TextView contentTv;

    private TextView positiveTv;

    private TextView negativeTv;

    private EditText editText;


    public OnDialogClickListener onDialogClickListener;

    private String title;

    private String content;

    private String positiveStr;

    public String negativeStr;

    public String popType = "";
    public  boolean showEditText;

    private View dv;

    boolean isCancelable = false;



    private void setTitle(String title)
    {
        if (!TextUtils.isEmpty(title))
        {
            titleTv.setText(Html.fromHtml(title));
            titleTv.setVisibility(View.VISIBLE);
        }
        else
        {
            titleTv.setVisibility(View.GONE);
        }

    }

    private void setContent(String content)
    {
        if (!TextUtils.isEmpty(content))
        {
            contentTv.setText(Html.fromHtml(content));
            contentTv.setVisibility(View.VISIBLE);
        }
        else
        {
            contentTv.setVisibility(View.GONE);
        }
    }

    private void setPositive(String positive)
    {
        if (!TextUtils.isEmpty(positive))
        {
            positiveTv.setText(positive);
        }
        else
        {
            positiveTv.setVisibility(View.GONE);
        }
    }

    private void setNegative(String negative)
    {
        if (!TextUtils.isEmpty(negative))
        {
            if (this.positiveStr == null)
            {
                negativeTv.setBackgroundResource(R.drawable.circle_corner_square_btn_big_life);
            }
            negativeTv.setText(negative);
        }
        else
        {
            negativeTv.setVisibility(View.GONE);
        }
    }
    private void showEditText()
    {
        if (showEditText)
        {
            editText.setVisibility(View.VISIBLE);
            editText.requestFocus();
        }
    }

    @Override
    public void onClick(View v)
    {
        int viewID = v.getId();
        if (viewID == positiveTv.getId())
        {
            if (onDialogClickListener != null)
            {
                if (showEditText && editText.getText() != null)
                {
                    onDialogClickListener.onPositiveClickListener(popType,editText.getText().toString());
                }
                else
                {
                    onDialogClickListener.onPositiveClickListener(popType,null);
                }
            }

        }
        else if (viewID == negativeTv.getId())
        {
            if (onDialogClickListener != null)
            {
                if (showEditText && editText.getText() != null)
                {
                    onDialogClickListener.onNegativeClickListener(popType,editText.getText().toString());
                }
                else
                {
                    onDialogClickListener.onNegativeClickListener(popType,null);
                }

            }
        }

        this.finish();
        overridePendingTransition(android.R.anim.fade_in,R.anim.fade_out);

    }

    public static interface OnDialogClickListener
    {

        public void onPositiveClickListener(String type, String text);
        public void onNegativeClickListener(String type, String text);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.view_defaulting_dialog);

        this.title = this.getIntent().getStringExtra("title");
        this.content = this.getIntent().getStringExtra("content");
        this.positiveStr = this.getIntent().getStringExtra("positiveStr");
        this.negativeStr = this.getIntent().getStringExtra("negativeStr");
        this.showEditText = this.getIntent().getBooleanExtra("showEditText",false);
        this.popType = this.getIntent().getStringExtra("popType");
        this.isCancelable = this.getIntent().getBooleanExtra("isCancelable",false);
        titleTv = (TextView)this.findViewById(R.id.view_defaulting_dialog_titleTv);
        contentTv = (TextView)this.findViewById(R.id.view_defaulting_dialog_contentTv);
        positiveTv = (TextView)this.findViewById(R.id.view_defaulting_dialog_positiveTv);
        negativeTv = (TextView)this.findViewById(R.id.view_defaulting_dialog_negativeTv);
        editText = (EditText)this.findViewById(R.id.editText);
        dv = this.findViewById(R.id.dv);
        positiveTv.setOnClickListener(this);
        negativeTv.setOnClickListener(this);

        if (content == null)
        {
            dv.setVisibility(View.GONE);
        }

        setTitle(title);
        setContent(content);
        setPositive(positiveStr);
        setNegative(negativeStr);
        showEditText();
        onDialogClickListener = (BaseActivity) AppManager.getAppManager().getActivity();
        if (this.isCancelable)
        {
            LinearLayout root = (LinearLayout)findViewById(R.id.root);
            root.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    DefaultDialog.this.finish();
                    overridePendingTransition(android.R.anim.fade_in,R.anim.fade_out);
                }
            });
        }


    }


    @Override
    protected void onDestroy() {
        super.onDestroy();

    }


}
